package com.neuedu.dao.impl;

import com.neuedu.common.DBUtil;
import com.neuedu.dao.BaseDao;
import com.neuedu.dao.TableDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class TableDaoImpl extends BaseDao implements TableDao  {

    @Override
    public List<Map<String, Object>> gettable(Object[] parm) {
        String sql = "SELECT d.id,deptnum,deptname,classifyname,typename FROM deptment d,depttype t,deptclassify c WHERE d.depttypeid=t.id AND deptclassifyid=c.id ";
        if (parm.length == 2) {
            sql += " order by d.id desc limit ?,? ";
        } else if (parm.length == 4) {
            sql += "and (deptnum like ? or deptname like ?) order by d.id desc limit ?,? ";
        }
        Connection con = DBUtil.getCon();
        List<Map<String, Object>> list = null;
//        int[] parm1 = new int[parm.length];
//        for (int i = 0; i < parm.length; i++) {
//            parm1[i] = (int) parm[i];
//        }
        try {
            list = this.queryParmSqlAsName(sql, parm, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteCol(Object[] parm){
        String sql = "DELETE FROM deptment WHERE id=?";
        Connection con = DBUtil.getCon();
        int flag=0;
        try {
            flag=this.delUpins(sql,parm,con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public int updateCol(Object[] parm) {
        String sql = "UPDATE deptment SET deptnum=?,deptname=?,deptclassifyid=(SELECT id FROM deptclassify WHERE classifyname=?),depttypeid=(SELECT id FROM depttype WHERE typename=?) WHERE id=?";
        Connection con = DBUtil.getCon();
        int flag = 0;
        try {
            flag = this.delUpins(sql, parm, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public int getCount() {
        String sql = "select count(*) from deptment";
        Connection con = DBUtil.getCon();
        int count = -1;
        try {
            ResultSet rs = con.prepareStatement(sql).executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public int getCount(Object[] parm) {
        String sql;
        Connection con = DBUtil.getCon();
        String deptnum = parm[0].toString();
        String deptname = parm[1].toString();
        if (deptname == null || deptname.trim().length() == 0 || deptname.equals("undefined")) {
            sql = "select count(*) from deptment ";
        } else {
            sql = "select count(*) from deptment where (deptname like '%" + deptname + "%' or " + "deptnum like '%" + deptnum + "%')";
        }
        int count = -1;
        try {
            ResultSet rs = con.prepareStatement(sql).executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }


    @Override
    public List<Map<String, Object>> getLikeDept(String sql, Object[] objs) {
        Connection con = DBUtil.getCon();
        List<Map<String, Object>> list = null;
        try {
            list = this.queryParmSqlAsName(sql, objs, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
